package com.mystnihon.freeremote.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.mystnihon.freeremote.R;
import com.mystnihon.freeremote.ui.fragment.FileChooserFragment;
import com.mystnihon.freeremote.ui.fragment.UrlChooserFragment;

public class UploadActivity extends SherlockFragmentActivity implements OnCheckedChangeListener {
	public static final int REQUEST_CHOOSE_FILE = 10;
	public static final String EXTRA_NAME_FILTER = "EXTRA_NAME_FILTER";
	public static final String EXTRA_RESPONSE_URI = "EXTRA_RESPONSE_URI";
	public static final String EXTRA_SEARCH_TYPE = "EXTRA_SEARCH_TYPE";

	private enum FragmentAdd {
		File, Url
	}

	private RadioGroup mRGChoosedUpload;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_upload);

		Bundle bundle = null;
		Intent intent = getIntent();
		if (intent != null) {
			String mNameFilter = intent.getStringExtra(EXTRA_NAME_FILTER);
		}

		mRGChoosedUpload = (RadioGroup) findViewById(R.id.fr_add_torrent_rd_grp);
		mRGChoosedUpload.setOnCheckedChangeListener(this);
		mRGChoosedUpload.check(R.id.fr_add_torrent_rd_btn_file);
		// switchFragment(FragmentAdd.File, bundle);

	}

	private void switchFragment(FragmentAdd type, Bundle bundle) {
		FragmentManager fm = getSupportFragmentManager();
		Fragment frag = fm.findFragmentById(R.id.fr_add_torrent_frame);
		switch (type) {
			case File:
				if (frag == null || !(frag instanceof FileChooserFragment)) {
					FragmentTransaction ft = fm.beginTransaction();
					FileChooserFragment fragment = FileChooserFragment.newInstance();
					if (bundle != null) {
						fragment.setArguments(bundle);
					}
					ft.replace(R.id.fr_add_torrent_frame, fragment);
					ft.commit();
				}
				break;
			case Url:
				if (frag == null || !(frag instanceof UrlChooserFragment)) {
					FragmentTransaction ft = fm.beginTransaction();
					UrlChooserFragment fragment = UrlChooserFragment.newInstance();
					if (bundle != null) {
						fragment.setArguments(bundle);
					}
					ft.replace(R.id.fr_add_torrent_frame, fragment);
					ft.commit();
				}
				break;
		}
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			FragmentManager fm = getSupportFragmentManager();
			Fragment frag = fm.findFragmentById(R.id.fr_add_torrent_frame);
			if (frag != null && frag instanceof FileChooserFragment && frag.isVisible()) {
				if (((FileChooserFragment) frag).goBack()) {
					return true;
				}
			}
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		Bundle bundle = null;
		Intent intent = getIntent();
		if (intent != null) {
			String mNameFilter = intent.getStringExtra(EXTRA_NAME_FILTER);
		}
		switch (checkedId) {
			case R.id.fr_add_torrent_rd_btn_file:
				switchFragment(FragmentAdd.File, bundle);
				break;
			case R.id.fr_add_torrent_rd_btn_url:
				switchFragment(FragmentAdd.Url, bundle);
				break;
		}

	}
}
